import moment from 'moment';

/**
 * 获取当前时间
 * @returns {moment.Moment}
 */
export const getNowDate = (format: string) => moment(new Date()).format(format);

/**
 * 格式化时间
 * @param data
 * @param format
 */
export const formatDate = (data: string, format: string) => {
  if (!data) {
    return '';
  }
  return moment(data).format(format);
};

/**
 * 当前时间之前的几天
 * @param day
 * @returns {moment.Moment}
 */
export const getPreDayDate = (day: number) => {
  const curTime = new Date().getTime();
  const date2 = curTime - day * 3600 * 24 * 1000;
  const nDate = new Date(date2);
  return moment(nDate);
};

/**
 * 当前时间之前的一个月
 * @returns {moment.Moment}
 */
export const getBeforeDate = () => {
  const date = new Date();
  let time = date.getTime();
  time -= 30 * 86400000;
  const nDate = new Date(time);
  return moment(nDate.toLocaleDateString());
};

/**
 * 当前时间之前的几周
 * @param week
 * @returns {*|moment.Moment}
 */
export const getPreWeekDate = (week: number) => {
  const nowDate = new Date().getTime();
  const date2 = nowDate - week * 7 * 24 * 60 * 60 * 1000;
  const nDate = new Date(date2);
  return moment(nDate);
};

/**
 * 当前时间之前的几年
 * @param year
 * @returns {*|moment.Moment}
 */
export const getBeforeYearDate = (year: number) => {
  const date = new Date();
  date.setFullYear(date.getFullYear() - year);
  return moment(new Date(date));
};
/**
 * 获取指定时间那天的 00:00:00开始的时间
 * @param data
 * @returns {string}
 */
export const getStartDate = (data: { format: (arg0: string) => any }) => {
  if (!data) {
    return '';
  }
  return `${data.format('YYYY-MM-DD')} 00:00:00`;
};

/**
 * 获取指定时间的 23:59:59结束的时间
 * @param data
 * @returns {string}
 */
export const getEndDate = (data: { format: (arg0: string) => string }) => {
  if (!data) {
    return '';
  }
  return `${data.format('YYYY-MM-DD')} 23:59:59`;
};
